// pages/alarm-detail/index.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        visible: false,
        alarm: null,
        dismissAlarm: true,
        record: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let faultId = options.faultId
        let isRead = options.isRead
        if (faultId != null) {
            app.api.getAlarmDetail(faultId).then(res => {
                this.setData({
                    alarm: res.data
                })
            })
        }
        if (isRead != null && isRead == '0') {
            app.api.updateAlarmRead(faultId).then(res => {
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    onVisibleChange(e) {
        this.setData({
            visible: e.detail.visible,
        });
    },
    handlerAlarm() {
        this.setData({
            visible: true,
            record: ''
        });
    },
    dismissAlarm() {
        this.setData({record: '一键消警'});
    },
    oneKeyReport() {
        wx.showToast({
            title: '已报修'
        })
    },
    contactAdmin() {
        wx.makePhoneCall({
            phoneNumber: '132123456',
        })
    },
    onVisibleChange(e) {
        this.setData({
            visible: e.detail.visible,
        });
    },
    cancelPop() {
        this.setData({visible: false});
    },
    okPop() {
        this.submitData()
    },
    switchChange(e) {
        console.log(e);
        this.setData({
            dismissAlarm: e.detail.value
        })
    },
    changeContext(e) {
        this.setData({
            record: e.detail.value
        })
    },
    submitData() {
        let data = {}
        data.faultId = this.data.alarm.faultId
        data.record = this.data.record
        data.isDismiss = this.data.dismissAlarm ? "1" : "0"
        app.api.dismissAlarm(data).then(res => {
            wx.showToast({
                title: '处理成功',
            })
            if (this.data.visible) {
                this.setData({
                    visible: false
                })
            }
            app.api.getAlarmDetail(this.data.alarm.faultId).then(res => {
                this.setData({
                    alarm: res.data
                })
            })
        })
    }
})